package com.xt.purchasingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xt.purchasingsystem.dto.BuyerExecuteDto;
import com.xt.purchasingsystem.dto.CarGoDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xt.purchasingsystem.dto.ReleaseCargoDto;
import com.xt.purchasingsystem.mapper.BuyerExecuteDetailMapper;
import com.xt.purchasingsystem.mapper.BuyerExecuteMapper;
import com.xt.purchasingsystem.mapper.ReleaseCargoDetailMapper;
import com.xt.purchasingsystem.mapper.ReleaseCargoMapper;
import com.xt.purchasingsystem.model.BuyerExecuteDetail;
import com.xt.purchasingsystem.model.ReleaseCargo;
import com.xt.purchasingsystem.model.ReleaseCargoDetail;
import com.xt.purchasingsystem.service.ReleaseCargoService;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.xt.purchasingsystem.utils.IdWorker;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yhp
 * @since 2020-05-31
 */
@Service
@Transactional
public class ReleaseCargoServiceImpl extends ServiceImpl<ReleaseCargoMapper, ReleaseCargo> implements ReleaseCargoService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ReleaseCargoMapper releaseCargoMapper;
    @Autowired
    private ReleaseCargoDetailMapper releaseCargoDetailMapper;
    @Autowired
    private BuyerExecuteDetailMapper buyerExecuteDetailMapper;
    @Autowired
    private BuyerExecuteMapper buyerExecuteMapper;

    @Override
    public Result saveReleaseCargoAndDetails(ReleaseCargoDto releaseCargoDto) {
        String id = String.valueOf(idWorker.nextId());
        releaseCargoDto.getReleaseCargo().setId(id);
        CompletableFuture.runAsync(() -> {
            releaseCargoMapper.insert(releaseCargoDto.getReleaseCargo());
        }, threadPoolExecutor);

        CompletableFuture.runAsync(() -> {
            for (ReleaseCargoDetail releaseCargoDetail : releaseCargoDto.getReleaseCargoDetails()) {
                releaseCargoDetail.setId(String.valueOf(idWorker.nextId()));
                releaseCargoDetail.setReleaseCargoId(id);
                releaseCargoDetailMapper.insert(releaseCargoDetail);
            }
        }, threadPoolExecutor);

        CompletableFuture.runAsync(() -> {
            for (BuyerExecuteDetail buyerExecuteDetail : releaseCargoDto.getBuyerExecuteDetails()) {
                buyerExecuteDetailMapper.updateById(buyerExecuteDetail);
            }

            List<BuyerExecuteDetail> allBuyerExecuteDetailByBuyerExecuteId = buyerExecuteDetailMapper.getAllBuyerExecuteDetailByBuyerExecuteId(releaseCargoDto.getBuyerExecuteDetails().get(0).getBuyerExecuteId());
            List<BuyerExecuteDetail> collect = allBuyerExecuteDetailByBuyerExecuteId.stream().filter(b -> b.getRegisterTag().equals("R001-0")).collect(Collectors.toList());
            if (collect.size() == 0) {
                buyerExecuteMapper.updatePutTagById(releaseCargoDto.getBuyerExecuteDetails().get(0).getBuyerExecuteId(), "F001-1");
            }

        }, threadPoolExecutor);


        return ResultFactory.buildSuccessResult("添加成功,需要审核!");
    }

    @Override
    public Result getAllReleaseCargoAndDetailsIsNoCheck(Integer currNo, Integer pageSize) {

        PageHelper.startPage(currNo, pageSize);
        List<ReleaseCargoDto> allReleaseCargoAndDetailsIsNoCheck = releaseCargoMapper.getAllReleaseCargoAndDetailsIsNoCheck();
        PageInfo<ReleaseCargoDto> pageInfo = new PageInfo<>(allReleaseCargoAndDetailsIsNoCheck);
        return ResultFactory.buildSuccessResult("查询成功", pageInfo);

    }
    @Override
    public Result qualityInspectionResults(Integer currNo, Integer pageSize) {
        PageHelper.startPage(currNo, pageSize);
        List<ReleaseCargoDto> allBuyerExecuteNoCheck = releaseCargoMapper.qualityInspectionResults();
        PageInfo<ReleaseCargoDto> pageInfo = new PageInfo(allBuyerExecuteNoCheck);
        return ResultFactory.buildSuccessResult("查询成功", pageInfo);
    }
    @Override
    public Result checkReleaseCargAndDetails(ReleaseCargoDto releaseCargoDto) {
        //审核通过
        releaseCargoMapper.updateById(releaseCargoDto.getReleaseCargo());
        if (!releaseCargoDto.getReleaseCargo().getCheckTag().equals("R001-1")) {
            //审核不通过
            CompletableFuture.runAsync(() -> {
                QueryWrapper<BuyerExecuteDetail> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("BUYER_EXECUTE_ID", releaseCargoDto.getReleaseCargo().getBuyerPlanId()).eq("SUPPLIER_ID", releaseCargoDto.getReleaseCargo().getSupplierId());
                BuyerExecuteDetail buyerExecuteDetail = buyerExecuteDetailMapper.selectOne(queryWrapper);
                buyerExecuteDetail.setRegisterTag("R001-0");
                buyerExecuteDetail.setRegisterAmount(buyerExecuteDetail.getRegisterAmount() - releaseCargoDto.getReleaseCargo().getSumNumber());
                buyerExecuteDetailMapper.updateById(buyerExecuteDetail);
            }, threadPoolExecutor);

            CompletableFuture.runAsync(() -> {
                buyerExecuteMapper.updatePutTagById(releaseCargoDto.getReleaseCargo().getBuyerPlanId(), "F001-0");
            }, threadPoolExecutor);
        }
        return ResultFactory.buildSuccessResult("审核成功");
    }
}
